package cn.cloudreal.cbms.contract.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.contract.dao.ContractListDao;
import cn.cloudreal.cbms.mediaPrice.dao.impl.MediaPriceDaoimpl;
import cn.cloudreal.cbms.vo.Contract;
import cn.cloudreal.cbms.vo.MediaPrice;

public class ContractListDaoImpl extends SqlMapClientDaoSupport implements ContractListDao{
   
	/** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(MediaPriceDaoimpl.class);
	private final String FIND_ContractList = "contract.findContractList"; //查询合同列表
	private final String FIND_countContract = "contract.countContract";   //统计合同数量
    private final String INSERT_Contract = "contract.insertContract";     //插入合同
    private final String DELETE_Contract ="contract.deleteContract";      //删除合同
    private final String DETAIL_Contract ="contract.detailContract";      //查看合同详细情况
	private final String FIND_ContractByYears = "contract.findContractByYears"; //根据年度查看合同
	private final String UPDATA_Contract ="contract.updataContract";      // 更新合同的数据
	private final String FIND_MediaName ="contract.seclectMediaName";     //查询媒体名称
	private final String FIND_IsExistContractID ="contract.seclectisExistID";     //查询数据库中是否存在此id
	
	private final String FIND_MediaPrice ="contract.seclectMediaPrice";     //查询媒体价格表   
	
	
    /**
     * 分页查询合同列表
     */
	public List<Contract> findContractList(PageBean page, Contract contract) {
	     // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ContractListDaoImpl findContractList() method");
        }
        //分页功能
        int totalCount = (Integer) getSqlMapClientTemplate().queryForObject(FIND_countContract,contract);
        page.setTotalRecords(totalCount);
        if (totalCount == 0)
        {
            return null;
        }
        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("pageno", page.getPageNo());
//        param.put("pagesize", page.getPageSize());
        
        param.put("begin", page.getPageSize() * (page.getPageNo() - 1) + 1);
        param.put("end", page.getPageSize() * page.getPageNo());
        
        param.put("contractYear", contract.getContractYear()); //合同的年度
        param.put("contractCity", contract.getContractCity()); //归属地市
        param.put("contractLevel", contract.getContractLevel()); //签约级别
        param.put("contractDepartment", contract.getContractDepartment()); //部门科室
        
        param.put("contractBName", contract.getContractBName()); //供应商名称
        param.put("contractOffice", contract.getContractOffice());//签约人
        param.put("contractName", contract.getContractName());//合同名称
        List<Contract> list = this.getSqlMapClientTemplate().queryForList(FIND_ContractList, param);
        
     // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ContractListDaoImpl findContractList() method");
        }
        return list;
	}
    
	/**
     * 插入合同的数据
     */
	public void addContract(Contract contract) {
		try {
			 // 打印进入方法体日志
	        if (log.isInfoEnabled())
	        {
	            log.info("Enter ContractListDaoImpl addContract() method");
	        }
	        getSqlMapClientTemplate().insert(INSERT_Contract,contract);
	        // 打印退出方法体日志
	        if (log.isInfoEnabled())
	        {
	            log.info("Exit ContractListDaoImpl addContract() method");
	        }
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * 根据年度查看合同
	 * 
	 */
	public List<Contract> findContractByYears(Contract contract)
    {
		 // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ContractListDaoImpl findContractByYears() method");
        }
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("years", contract.getContractYear());
        param.put("contractName", contract.getContractName());
        param.put("contractID", contract.getContractID());
        param.put("contractCity", contract.getContractCity());
        param.put("begTime", contract.getContractStartTime());//开始时间
        param.put("endTime", contract.getContractEndTime());//开始时间
        List<Contract> list = this.getSqlMapClientTemplate().queryForList(FIND_ContractByYears, param);
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ContractListDaoImpl findContractByYears() method");
        }
        return list;
        
    }

	/*
	 * 删除合同
	 * 
	 */
	public void deleteContract(String contractId) {
	     // 打印进入方法体日志
		try{
	        if (log.isInfoEnabled())
	        {
	            log.info("Enter ContractListDaoImpl deleteContract() method");
	        }
	        getSqlMapClientTemplate().delete(DELETE_Contract,contractId);
	        // 打印退出方法体日志
	        if (log.isInfoEnabled())
	        {
	            log.info("Exit ContractListDaoImpl deleteContract() method");
	        }
		}
		catch (Exception e) {
		}
	}
	/*
	 * (non-Javadoc)查看合同的详细信息
	 * 
	 */
	public Contract detailContract(String contractId)
	{
	     // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ContractListDaoImpl detailContract() method");
        }
        Contract contract =  (Contract) getSqlMapClientTemplate().queryForObject(DETAIL_Contract,contractId);
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ContractListDaoImpl detailContract() method");
        }
        return contract;
	}
	
	/*
	 * 更新合同的信息
	 */
	public void updataContract(Contract contract) {
	     // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ContractListDaoImpl updataContract() method");
        }
       getSqlMapClientTemplate().update(UPDATA_Contract, contract);
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ContractListDaoImpl updataContract() method");
        }
	}


	   /*
	    * 通过合同编号查询媒体名称
	    * 
	    */
	public List<String> seclectMediaName(String contractId) {
	     // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ContractListDaoImpl seclectMediaName() method");
        }
        List<String> listMediaName = null;
        listMediaName = this.getSqlMapClientTemplate().queryForList(FIND_MediaName, contractId);
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit ContractListDaoImpl seclectMediaName() method");
        }
		return listMediaName;
	}

    /*
     * 查询合同判断合同编号是否已经存在
     */
	public List<Contract> isEixstContractID(String contractId,String contractName) {
	     // 打印进入方法体日志
       if (log.isInfoEnabled())
       {
           log.info("Enter ContractListDaoImpl isEixstContractID() method");
       }
       List<Contract> listContractID = null;
       Map<String, Object> param = new HashMap<String, Object>();
       param.put("contractID", contractId);
       param.put("contractName", contractName);
       listContractID = this.getSqlMapClientTemplate().queryForList(FIND_IsExistContractID, param);
       // 打印退出方法体日志
       if (log.isInfoEnabled())
       {
           log.info("Exit ContractListDaoImpl isEixstContractID() method");
       }
		return listContractID;
	}

	/**
	 * 通过合同的id查询媒体价格表
	 */
	public List<MediaPrice> seclectMediaPrice(String contractId) {
		
	     // 打印进入方法体日志
	       if (log.isInfoEnabled())
	       {
	           log.info("Enter ContractListDaoImpl isEixstContractID() method");
	       }
	       List<MediaPrice> listMediaPrice = null;
	       listMediaPrice = this.getSqlMapClientTemplate().queryForList(FIND_MediaPrice, contractId);
	       // 打印退出方法体日志
	       if (log.isInfoEnabled())
	       {
	           log.info("Exit ContractListDaoImpl isEixstContractID() method");
	       }
			return listMediaPrice;
		
	}



	



}
